

var frontBaseUrl = "http://localhost:5500";
var backBaseUrl = "http://localhost:8080";
var maxPage = 1;
function loadData(){
    loadBoardPage(1);
}
function loadBoardPage(num){
    $.ajax({
        url: backBaseUrl + '/board-message/page',
        method: 'POST',
        data: {
            // 每页几条数据,第几页
            "numPerPage":10,
            "pageNum":num
        },
        crossDomain: true,
        xhrFields: { withCredentials: true },
        success: function (msg) {
            let data = msg.data;
            console.log(data);
            attachItems(data.list);
            attachPager(data.pageList,num);
        }, error: function (error) {
            layer.msg("公告板加载失败,将在3秒后跳转回首页");
            window.setTimeout(function (){
                window.location.href='../home.html';
            },3000);
        }
    });


    // <div class="board-list-item">
    //     <span class="item-title">语扉论坛新建站！欢迎访问</span>
    //     <span class="create-time">2022-2-12 3:23:12</span>
    // </div>
}
function attachItems(list){
    $("#board-list").html("");
    $(list).each(function(i,e){
        let item = "<div class='board-list-item'>";
        item += "<a href='./detail.html?id=" + e.id + "'>";
        item += "<span class='item-title'>" + e.title + "</span></a>";
        item += "<span class='create-time'>" + e.createTime + "</span>";
        item += "</div>";
        $("#board-list").append(item);
    });
}
function attachPager(pageList,num){
    $(".page-list").html("");
    maxPage = parseInt(pageList[pageList.length-1]);
    $(pageList).each(function(i,e){
        let str = "<div class="
        if(e === num.toString()){
            str += "'my-button-now'";
        }else{
            str += "'my-button'";
        }
        if(e !== '...'){
            str += "onclick='loadBoardPage(" + e + ")'"
        }
        str += "/>" + e + "</div>";
        $(".page-list").append(str);
    })
}

function lastPage(){
    let tPage =  $(".my-button-now").text();
    console.log(tPage)
    let nowPage = parseInt(tPage);
    if(nowPage <= 1){
        // layer.msg("没有了");
        return;
    }
    loadBoardPage(nowPage - 1);
}

function nextPage(){

    let tPage =  $(".my-button-now").text();
    console.log(tPage)
    let nowPage = parseInt(tPage);
    if(nowPage >= maxPage){
        // layer.msg("没有了");
        console.log("没有了 ")
        return;
    }
    loadBoardPage(nowPage +1);
}

function jumpToPage(){
    let tPage = $("#jump-input").val();
    console.log(tPage);
    let page = parseInt(tPage);
    console.log(typeof(page))
    console.log(page)
    if(page < 1 || page > maxPage || isNaN(page)){
        return;
    }
    loadBoardPage(page);
}
